import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/login',
    component: () =>
        import ('../views/Login/Login.vue')
}, {
    path: '/home',
    component: () =>
        import ('../views/Home.vue')
}]

const router = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes,
    })
    // 全局路由前置守卫
router.beforeEach((to, from, next) => {
    // 访问login直接放行
    if (to.pash != '/login') {
        next()
    } else {
        //除了login之外的路由路径访问都要拦截
        if (sessionStorage.getItem('token')) {
            alert('未登录 请先登录')
            next('/login')
        } else {
            next()
        }
    }
})
export default router